﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;

namespace PeisongWeb.admin
{
    public partial class smsMessageList : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("DL", 1))
                {
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }

        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.AppendFormat(" [source]='{0}'", ConfigurationManager.AppSettings["SMS_SOURCE"]);
            if (ext_StartInputDt.SelectedDate != DateTime.MinValue)
                where.AppendFormat(" and datediff(d,tim,'{0}')<=0", ext_StartInputDt.SelectedDate);
            if (ext_EndInputDt.SelectedDate != DateTime.MinValue)
                where.AppendFormat(" and datediff(d,tim,'{0}')>=0",ext_EndInputDt.SelectedDate);

            DataSet ds = logic.sendSMS.select(where.ToString(), "tim desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        protected void btnNewMessage_Click(object sender, EventArgs e)
        {
            winMessage.Show();
        }

        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "View")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                getDetail(id);
                errorDetails.Show();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void lbtn_Search_Click(object sender,EventArgs e)
        {
            rptBind();
        }

        void getDetail(int id)
        {

            DataRow dr = DAL.DbHelper2000.ExecuteDataRow("[dbo].[p_Ab_Send_getById]", id);
            if (dr != null)
            {
                lbl_id.Text = dr["id"].ToString();
                lbl_tim.Text = dr["tim"].ToString();
                lbl_cont.Text = dr["cont"].ToString();
                lbl_phns.Text = dr["phns"].ToString();
                lbl_flag.Text = dr["flag"].ToString() == "0" ? "未发送" : "已发送";
                lbl_Url.Text = dr["url"].ToString();
            }
        }

        protected void btn_Save_Click(object sender,EventArgs e)
        {
            string phones = txt_Phones.Text;

            logic.sendSMS.send_SMS(phones, txt_Content.Text.Replace("<br/>", "\n"));
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
            winMessage.Close();
        }
    }
}
